const connectionPoll = require("../app/mysql");

class momentService{
     //查询数据:传入query数据进行查询
   async momentList(offset = 0,limit = 10)
    {
      //从数据库查找对对应的数据
      console.log("offset", offset, "limit:", limit);
        const statement = "select * from t_moment limit ? offset ?";
        //查找得到result
        const [result]= await connectionPoll.promise().execute(statement, [limit, offset]);
      
      //将结果返回出去
        return result;
    }
    
    async momentSearchById(id)
    {
        if (!id) 
        {
           return ""    
        }
        //通过id查找数据
        const statement = "select content from t_moment where id = ?";
        const [result] = await connectionPoll.promise().execute(statement, [id]);
        return result[0];
    }

    async momentUpdate(content,id)
    {
        const statement = "update t_moment set content = ? where id = ?";
        const [result] = await connectionPoll.promise().execute(statement, [content, id]);
        return result.length;
    }

    async momentRemove(id)
    {
        const statement = "delete from t_moment where id = ?";
        const [result] = await connectionPoll.promise().execute(statement, [id]);
        return result.length;
    }
}
    
module.exports = new momentService();